//
//  DistrictView.m
//  MemberProject
//
//  Created by mac on 15/4/30.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "DistrictView.h"
#import "CityPositionCollectionViewCell.h"
#import "NowPositionCollectionViewCell.h"
@implementation DistrictView

-(void)awakeFromNib{
    self.collectionView.delegate=self;
    self.collectionView.dataSource=self;
    self.backgroundColor=[UIColor clearColor];
    self.collectionView.backgroundColor=[UIColor colorWithRed:236/255.0 green:235/255.0 blue:232/255.0 alpha:1];
    [self.collectionView registerNib:[UINib nibWithNibName:@"CityPositionCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CellIdentifier"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"NowPositionCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"NowCellIdentifier"];
    
    
    self.tapView.backgroundColor=[UIColor colorWithRed:29/255.0 green:29/255.0 blue:29/255.0 alpha:0.7];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    [self.tapView addGestureRecognizer:tap];
}

#pragma mark - tap
-(void)tap:(UITapGestureRecognizer *)sender{
    self.hidden=YES;
}

#pragma mark - collectionView
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if(section==0){
        return self.data.count+1;
    }
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier=@"CellIdentifier";
    static NSString *NowCellIdentifier=@"NowCellIdentifier";
    if(indexPath.section==1){
        NowPositionCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:NowCellIdentifier forIndexPath:indexPath];
        cell.cityLabel.text=self.cityLabelText;
        return cell;
    }else{
        CityPositionCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
        if(indexPath.row==self.lastSelectedIndexPath.row){
            cell.is_Selected=YES;
        }else{
            cell.is_Selected=NO;
        }
        if(indexPath.row==0){
            cell.textLabel.text=@"全城";
        }else{
            NSDictionary *dic=self.data[indexPath.row-1];
            if(dic[@"s"]){
                cell.textLabel.text=dic[@"s"];
            }else{
                cell.textLabel.text=dic[@"n"];
            }
            
        }
        return cell;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==0){
        NSString *name=nil;
        if(indexPath.row==0){
            name=self.dic[@"name"];
        }else{
            NSDictionary *dic=self.data[indexPath.row-1];
            if(dic[@"s"]){
                name=dic[@"s"];
            }else{
                name=dic[@"n"];
            }
        }
        if(self.lastSelectedIndexPath==nil){
            self.lastSelectedIndexPath=indexPath;
            [collectionView reloadData];
        }else{
            NSIndexPath *tempIndexPath=self.lastSelectedIndexPath;
            if(tempIndexPath.row!=indexPath.row){
                self.lastSelectedIndexPath=indexPath;
                [collectionView reloadItemsAtIndexPaths:@[tempIndexPath,self.lastSelectedIndexPath]];
            }
            if([self.delegate respondsToSelector:@selector(districtView:indexPath:positionDic:)]){
                [self.delegate districtView:self indexPath:indexPath positionDic:@{@"name":name}];
            }
        }
        
        return;
    }
    if(indexPath.section==1){
        if([self.delegate respondsToSelector:@selector(districtView:indexPath:positionDic:)]){
            [self.delegate districtView:self indexPath:indexPath positionDic:@{@"name":@"name"}];
        }
        return;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==0){
        return CGSizeMake((collectionView.width-15*4)/3, 40);
    }
    return CGSizeMake(SCREEN_WIDTH-15*2, 40);
}

-(void)refreshHeightOfDistrictView{
    self.collectionViewHeight.constant=[self getHeightOfDistrictViewWithArray:self.data];
    if(self.collectionViewHeight.constant>SCREEN_HEIGHT-64-49){
        self.collectionViewHeight.constant=SCREEN_HEIGHT-64-49;
    }
}

-(CGFloat)getHeightOfDistrictViewWithArray:(NSArray *)array{
    float height=0;
    int linesCount=0;
    int count=(int)array.count+1;
    linesCount=count/3;
    if(count%3!=0){
        linesCount++;
    }
    height+=10+10+linesCount*40+7*(linesCount-1);
    height+=10+10+40;
    return height;
}

@end
